Product Code Database
Example Keywords: tie -super $70
   » » Wiki: Hash Consing
Tag Wiki 'Hash Consing'.
Tag

In , particularly in functional programming, hash consing is a technique used to share values that are structurally equal. When a value is constructed, such as a cell, the technique checks if such a value has been constructed before, and if so reuses the previous value, avoiding a new memory allocation. A useful property of hash consing is that two structures can be tested for equality in constant time via pointer equality, which in turn can improve efficiency of divide and conquer algorithms when data sets contain overlapping blocks. Hash consing has been shown to give dramatic performance improvements—both space and time—for symbolic and dynamic programming algorithms.

Hash consing is most commonly implemented with storing that may be garbage-collected when the data stored therein contains no references from outside the table.

(1978). 007001115X, . 007001115X


Example
The following is a simple (though inefficient) demonstration of a by means of hash table and weak references in Scheme. The bwp-object function returns true if the reference given is a broken weak pointer, i.e., the target has been garbage-collected.

; weak hashes
;
(require 'hash-table)

(define (make-weak-table . args)

 (apply make-hash-table args))
     

(define (weak-table-set! table key data)

 (let ((w (hash-table-ref table key #f)))
   (if w
       (vector-set! w 0 data)
     (let ((w (make-weak-vector 1)))
       (vector-set! w 0 data)
       (hash-table-set! table key w)))))
     

(define (weak-table-ref table key)

 (let ((w (hash-table-ref table key #f)))
   (if w
       (vector-ref w 0)
     #f)))
     

; memoizer factory
for given (side-effect-free) procedure,
; return a procedure which does the same memoizing some of results
; in the sense of equal? on the whole list of args
;
(define (make-weak-memoizer proc)
 (let ((cache (make-weak-table equal?)))
   (lambda args
     (let ((x (weak-table-ref cache args)))
       (if (bwp-object? x)
           (let ((r (apply proc args)))
             (weak-table-set! cache args r)
             r)
           x)))))
     


History
A hash consing compilation technique was presented by in 1958. The term "hash consing" originates from implementations in the context of Lisp in the 1970s.


See also


Further reading
  • Jean Goubault. Implementing Functional Languages with Fast Equality, Sets and Maps: an Exercise in Hash Consing. In Journées Francophones des Langages Applicatifs (JFLA’93), pages 222–238, Annecy, February 1993.

Page 1 of 1
1
Page 1 of 1
1

Account

Social:
Pages:  ..   .. 
Items:  .. 

Navigation

General: Atom Feed Atom Feed  .. 
Help:  ..   .. 
Category:  ..   .. 
Media:  ..   .. 
Posts:  ..   ..   .. 

Statistics

Page:  .. 
Summary:  .. 
1 Tags
10/10 Page Rank
5 Page Refs
1s Time